Conditions | 2 |
Paths | 16 |
Total Lines | 58 |
Lines | 58 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* TODO |
||
52 | View Code Duplication | app.get('/grades/', function (req, res, next) { |
|
53 | if (program.fulllog) { |
||
54 | var start = new Date(); |
||
55 | console.log((timeStamp() + 'Started to query the grades: ').cyan + req.query.id.yellow); |
||
1 ignored issue
–
show
|
|||
56 | } |
||
57 | access.login(req.query.id, req.query.pwd, res, function (headers, ires) { |
||
58 | program.fulllog && console.log((timeStamp() + 'Successfully logged in.').green); |
||
1 ignored issue
–
show
|
|||
59 | var ret = {}; |
||
60 | var $ = cheerio.load(ires.text); |
||
61 | ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].replace('<', '').substring(3); |
||
62 | ret.id = req.query.id; |
||
63 | |||
64 | // 进入成绩页面 |
||
65 | superagent.get(base + $('li[title="我的成绩"] a').attr('href')) |
||
66 | .set(headers) |
||
67 | .end(function (err, iires) { |
||
68 | if (err) { |
||
69 | console.log((timeStamp() + 'Failed to get grades page\n' + err.stack).red); |
||
1 ignored issue
–
show
|
|||
70 | res.send({ error: '无法进入成绩页面' }); |
||
71 | return next(err); |
||
72 | } |
||
73 | program.fulllog && console.log((timeStamp() + 'Successfully entered grades page.').green); |
||
74 | |||
75 | $ = cheerio.load(iires.text); |
||
76 | |||
77 | // 获取成绩列表 |
||
78 | let grades = {}; |
||
79 | let failed = {}; |
||
80 | $('#dataList').each(function (index) { |
||
81 | // cheerio没有实现jQuery的lt |
||
82 | if (index >= 2) |
||
83 | return; |
||
84 | $(this).find('tr[class!="theadCss"]').each(function() { |
||
85 | // 这段写得真是要吐血了 |
||
86 | let subject = escaper.unescape($(this).find('td[align="left"]').eq(1).text()); |
||
87 | if (subject) { |
||
88 | let score = $(this).find('font'); |
||
89 | if (score.text()) |
||
90 | grades[subject] = score.text(); |
||
91 | if (score.css('color')) |
||
92 | failed[subject] = score.text(); |
||
93 | } |
||
94 | }); |
||
95 | }); |
||
96 | ret.grades = grades; |
||
97 | ret['subject-count'] = Object.getOwnPropertyNames(grades).length; |
||
98 | ret.failed = failed; |
||
99 | ret['failed-count'] = Object.getOwnPropertyNames(failed).length; |
||
100 | |||
101 | // 完成所有工作后,登出 |
||
102 | access.logout(headers, res, function() { |
||
103 | // 第五步:返回JSON |
||
104 | res.send(JSON.stringify(ret)); |
||
105 | program.fulllog && console.log((timeStamp() + 'Successfully logged out: ').green + req.query.id.yellow + (' (processed in ' + (new Date() - start) + 'ms)').green); |
||
1 ignored issue
–
show
|
|||
106 | }); |
||
107 | }); |
||
108 | }); |
||
109 | }); |
||
110 | |||
168 | console.log((timeStamp() + 'The server is now running on port ' + port + '.').green); |